package com.renjianbo.project.module.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.renjianbo.project.common.model.PageReq;
import com.renjianbo.project.module.system.mapper.SysRolePermissionMapper;
import com.renjianbo.project.module.system.model.sys_role.*;
import com.renjianbo.project.module.system.mapper.SysRoleMapper;
import com.renjianbo.project.module.system.model.sys_role_permission.SysRolePermissionDTO;
import com.renjianbo.project.module.system.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.renjianbo.project.common.model.PageInfo;
import com.renjianbo.project.common.util.JsonUtil;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2022-04-19
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;


    /**
    * 查询数据
    */
    @Override
    public List<SysRoleDTO> list(SysRoleSearchReq req) {
        List<SysRoleDTO> list = sysRoleMapper.list(req);

        return list;
    }

    /**
    * 新增
    */
    @Override
    @Transactional
    public void add(SysRoleAddReq req){
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(req, sysRole);
        sysRole.setSort(sysRoleMapper.getMaxSort());

        sysRoleMapper.insert(sysRole);
    }

    /**
    * 删除
    */
    @Override
    @Transactional
    public void delete(Long id){
        SysRole sysRole = new SysRole();
        sysRole.setId(id);
        sysRole.setIsDelete(1);

        sysRoleMapper.updateById(sysRole);
    }

    /**
    * 更新
    */
    @Override
    @Transactional
    public void update(SysRoleUpdateReq req){
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(req, sysRole);

        sysRoleMapper.updateById(sysRole);
    }

    /**
    * 根据id查询
    */
    @Override
    public SysRoleDTO getById(Long id){
        SysRoleDTO sysRole = sysRoleMapper.getById(id);

        return sysRole;
    }

}
